using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;


namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FtpInstruction:InstructionBase
    {
        #region props
        private FtpProtocol _Protocol;
        /// <summary></summary>
        public FtpProtocol Protocol
        {
            get { return _Protocol; }
            set { _Protocol = value; }
        }

        private FtpDirection _Direction;
        /// <summary></summary>
        public FtpDirection Direction
        {
            get { return _Direction; }
            set { _Direction = value; }
        }

        private string _FtpSite;
        /// <summary></summary>
        public string FtpSite
        {
            get
            {
                return this._FtpSite;
            }
            set
            {
                this._FtpSite = value;
            }
        }

        private string _SrcPath;
        /// <summary></summary>
        public string SrcPath
        {
            get
            {
                return this._SrcPath;
            }
            set
            {
                this._SrcPath = value;
            }
        }

        private string _TgtFolder;
        /// <summary></summary>
        public string TgtFolder
        {
            get
            {
                return this._TgtFolder;
            }
            set
            {
                this._TgtFolder = value;
            }
        }

        private string _UserName;
        /// <summary></summary>
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        private string _Password;
        /// <summary></summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        private bool _Anonymous;
        /// <summary></summary>
        public bool Anonymous
        {
            get
            {
                return this._Anonymous;
            }
            set
            {
                this._Anonymous = value;
            }
        }

        private bool _WindowsAuth;
        /// <summary></summary>
        public bool WindowsAuth
        {
            get
            {
                return this._WindowsAuth;
            }
            set
            {
                this._WindowsAuth = value;
            }
        }
        #endregion
        /// <summary></summary>
        public FtpInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._FtpSite = string.Empty;
            this._Protocol = FtpProtocol.Ftp;
            this._Direction = FtpDirection.Download;
            this._SrcPath = string.Empty;
            this._TgtFolder = string.Empty;
            this._UserName = string.Empty;
            this._Password = string.Empty;
            this._Anonymous = false;
            this._WindowsAuth = false;
        }
        /// <summary></summary>
        public FtpInstruction(Guid componentID, Guid jobTicketID, 
            string site, FtpProtocol protocol, FtpDirection direction, 
            string srcFilePath, string tgtFolderPath, 
            string userName, string password,
            bool anonymous, bool windowsAuthentication) 
            : base(componentID, jobTicketID)
        {
            this._FtpSite = site;
            this._Protocol = protocol;
            this._Direction = direction;
            this._SrcPath = srcFilePath;
            this._TgtFolder = tgtFolderPath;
            this._UserName = userName;
            this._Password = password;
            this._Anonymous = anonymous;
            this._WindowsAuth = windowsAuthentication;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"FtpSite",this.FtpSite);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FtpProtocol", ((int) this.Protocol).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FtpDirection", ((int) this.Direction).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"SrcFilePath",this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"TgtFolderPath",this.TgtFolder);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "UserName", this.UserName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Password", this.Password);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"Anonymous",this.Anonymous.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WindowsAuth",this.WindowsAuth.ToString());

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FtpInstruction instruction = new FtpInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.FtpSite = xNode.Attributes.GetNamedItem("FtpSite").Value;
                instruction.Protocol = (FtpProtocol) int.Parse(xNode.Attributes.GetNamedItem("FtpProtocol").Value);
                instruction.Direction = (FtpDirection) int.Parse(xNode.Attributes.GetNamedItem("FtpDirection").Value);
                instruction.SrcPath = xNode.Attributes.GetNamedItem("SrcFilePath").Value;
                instruction.TgtFolder = xNode.Attributes.GetNamedItem("TgtFolderPath").Value;
                instruction.UserName = xNode.Attributes.GetNamedItem("UserName").Value;
                instruction.Password = xNode.Attributes.GetNamedItem("Password").Value;
                instruction.Anonymous = bool.Parse(xNode.Attributes.GetNamedItem("Anonymous").Value);
                instruction.WindowsAuth = bool.Parse(xNode.Attributes.GetNamedItem("WindowsAuth").Value);
                
                return instruction;
            }
            else
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
        }
    }
}
